﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using CloudTaobao.Util;

namespace CloudTaobao
{
    public partial class FindPage : PhoneApplicationPage
    {

        Find fd = new Find();
        private string query = "";
        private string snick = "";
        private string itemId = "";
        

        public FindPage()
        {
            InitializeComponent();
            fd.getItemFinishedEvent += new GetItemFinishedEventHandler(fd_getItemFinishedEvent);
            fd.startLoadingEvent += new StartLoadingEventHandler(fd_startLoadingEvent);
            fd.endLoadingEvent += new EndLoadingEventHandler(fd_endLoadingEvent);

            DataContext = fd;
            UserList.ItemsSource = fd.userList;
            ItemList.ItemsSource = fd.itemList;
            TradeRateList.ItemsSource = fd.tradeRateList;
        }

        void fd_endLoadingEvent()
        {
            LoadingBar.IsIndeterminate = false;
            LoadingBar.Visibility = Visibility.Collapsed;
        }

        void fd_startLoadingEvent(object source, StartLoadingEventArgs args)
        {
            LoadingBar.IsIndeterminate = true;
            LoadingBar.Visibility = Visibility.Visible;
        }

        void fd_getItemFinishedEvent(object source, GetItemFinishedEventArgs args)
        {
            if (args.wap_detail_url != null)
            {
                UriBuilder uri = new UriBuilder();
                uri.Query = "targetUrl=" + args.wap_detail_url;
                NavigationService.Navigate(new Uri("/WebBrowserPage.xaml" + uri.Query, UriKind.Relative));
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (searchbox.Text.Equals("")) 
            {
                return;
            }
            query = searchbox.Text;

            AppSettings settings = new AppSettings();
            settings.query = query;

            try
            {
                switch (pivotContrl.SelectedIndex)
                {
                    case 0:
                        fd.totalItems = 0;
                        fd.currentItemPage = 1;
                        fd.GetItemList(query, fd.currentItemPage, Utils.getRecordsNum(), Utils.isShowPic());
                        LoadingBar.Visibility = Visibility.Visible;
                        break;
                    case 1:
                        fd.GetUserInfo(query);
                        break;
                    case 2:
                        //fd.totalShops = 0;
                        //fd.currentShopPage = 1;
                        //fd.GetUserInfo(query);
                        break;
                    default:
                        return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void pivotContrl_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
        	// TODO: Add event handler implementation here.
            if (pivotContrl.SelectedIndex == 0)
                ApplicationBar = (Microsoft.Phone.Shell.ApplicationBar)Resources["itemBar"];
            else if (pivotContrl.SelectedIndex == 1)
                ApplicationBar = (Microsoft.Phone.Shell.ApplicationBar)Resources["infoBar"];
            else if (pivotContrl.SelectedIndex == 2)
                ApplicationBar = (Microsoft.Phone.Shell.ApplicationBar)Resources["commentBar"];
        }

        private void myInfo_Click(object sender, EventArgs e)
        {
            try
            {
                App app = Application.Current as App;
                if (!app.sessionInfo.ContainsKey("top_session") || app.sessionInfo["top_session"] == "")
                    return;
                string nick = app.sessionInfo["nick"];
                if (nick != null)
                {
                    fd.GetUserInfo(nick);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void previousItemPage_Click(object sender, EventArgs e)
        {
            try
            {
                if (fd.currentItemPage > 1 && query != "")
                {
                    fd.GetItemList(query, --fd.currentItemPage, Utils.getRecordsNum(), Utils.isShowPic());
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void nextItemPage_Click(object sender, EventArgs e)
        {
            try
            {
                if ((fd.currentItemPage * Utils.getRecordsNum() < fd.totalItems) && query != "")
                {
                    fd.GetItemList(query, ++fd.currentItemPage, Utils.getRecordsNum(), Utils.isShowPic());
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void filters_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/FilterSettings.xaml", UriKind.Relative));
        }

        private void itemList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {/*
            if (ItemList.SelectedItem == null) 
            {
                return;
            }
            try
            {

                string id = ((Item)ItemList.SelectedItem).num_iid;
                string nick = ((Item)ItemList.SelectedItem).nick;
                if (id != "")
                {
                    fd.GetItem(id, nick);
                }
                else
                {
                    MessageBox.Show("商品不存在");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }*/
        }

        private void Comments_Click(object sender, RoutedEventArgs e)
        {
            Item item = (Item)((Button)sender).DataContext;
            if (item == null)
            {
                return;
            }
            try
            {
                itemId = item.num_iid;
                snick = item.nick;
                if (itemId != "" && snick != "")
                {
                    
                    fd.totalTradeRate = 0;
                    fd.currentTradeRatePage = 1;
                    fd.GetTradeRateList(snick, itemId, fd.currentTradeRatePage, 20);
                    pivotContrl.SelectedIndex = 2;
                }
                else
                {
                    MessageBox.Show("商品不存在");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Buy_Click(object sender, RoutedEventArgs e)
        {
            Item item = (Item)((Button)sender).DataContext;
            if (item == null)
            {
                return;
            }
            try
            {

                string id = item.num_iid;
                string nick = item.nick;
                if (id != "")
                {
                    fd.GetItem(id, nick);
                }
                else
                {
                    MessageBox.Show("商品不存在");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void previousCommentPage_Click(object sender, EventArgs e)
        {
            try
            {
                if (fd.currentTradeRatePage > 1 && snick != "" && itemId != "")
                {
                    fd.GetTradeRateList(snick, itemId, --fd.currentTradeRatePage, 20);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void nextCommentPage_Click(object sender, EventArgs e)
        {
            try
            {
                if ((fd.currentTradeRatePage * 20 < fd.totalTradeRate) && snick != "" && itemId != "")
                {
                    fd.GetTradeRateList(snick, itemId, ++fd.currentTradeRatePage, 20);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /*
        private void Grid_MouseEnter(object sender, MouseEventArgs e)
        {
            try
            {
                ((Grid)sender).Background = new SolidColorBrush(Color.FromArgb(255, 240, 240, 240));
            }
            catch (Exception)
            {
                return;
            }
        }

        private void Grid_MouseLeave(object sender, MouseEventArgs e)
        {
            try
            {
                ((Grid)sender).Background = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255));
            }
            catch (Exception)
            {
                return;
            }
        }*/
    }
}